package com.vpen.picupload.controller;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.util.StrUtil;
import javazoom.jl.player.Player;
import org.apache.commons.io.FileUtils;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @version: 1.0
 * @author: Vpen
 * @date: 2021/9/3 23:03
 * @content:
 */
@RestController
@RequestMapping("/mp3")
public class ReadTimeMp3Controller {


    @PostMapping
    public Integer getTimeMp3(@RequestParam MultipartFile mp3) {




        int length = 0;
        File file = null;
        try {
            boolean valid = valid(mp3);
            System.out.println(valid);


            file = new File("1.mp3");

            FileUtils.copyInputStreamToFile(mp3.getInputStream(), file);

            MP3File mp3File = (MP3File) AudioFileIO.read(file);

            MP3AudioHeader audioHeader = (MP3AudioHeader) mp3File.getAudioHeader();
            // 单位为秒
            length = audioHeader.getTrackLength();
            return length;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(file);
            if (file != null) {
                file.delete();
            }


        }
        return length;
    }


    boolean valid(MultipartFile mp3) throws IOException {
        String filename = mp3.getOriginalFilename();


        if (StrUtil.isBlank(filename) || !filename.endsWith("mp3")) {

            return false;
        }
        System.out.println(mp3.getSize());

        return mp3.getSize() >= 0 && mp3.getSize() <= 4100097;


    }

//    public static void play(String position) {
//        try {
//            BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(position));
//            Player player = new Player(buffer);
//            player.play();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//    public static int getDuration(String position) {
//        int length = 0;
//        try {
//            MP3File mp3File = (MP3File) AudioFileIO.read(new File(position));
//            MP3AudioHeader audioHeader = (MP3AudioHeader) mp3File.getAudioHeader();
//            // 单位为秒
//            length = audioHeader.getTrackLength();
//            return length;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return length;
//    }
//    public static void main(String[] args) {
//        String position = "海底.mp3";
//        int sec = getDuration(position);
//        System.out.println(sec);
//        play(position);
//    }

}
